//
//  TrendChartView.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/3.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "TrendChartView.h"
#import "NSString+File.h"

#import "DaysGraphViewCell.h"
#import "DaysHistogramViewCell.h"

@interface TrendChartView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *trendTableView;
@property (nonatomic,strong)NSArray *countOnlineCarByDaily;
@property (nonatomic,strong)NSArray *countUseCarByDaily;
@property (nonatomic,strong)NSArray *increasedTrend;
@property (nonatomic,assign)BOOL allowLoad;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end

static NSString *DaysGraphViewCellID = @"DaysGraphViewCellID";
static NSString *DaysHistogramViewCellID = @"DaysHistogramViewCellID";
@implementation TrendChartView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
        _allowLoad = NO;
        _needRefresh = YES;
    }
    return self;
}
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (void)setUpUI
{
    
    _trendTableView = [UITableView new];
    _trendTableView.dataSource = self;
    _trendTableView.delegate = self;
    _trendTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _trendTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _trendTableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [_trendTableView registerClass:[DaysGraphViewCell class] forCellReuseIdentifier:DaysGraphViewCellID];
    [_trendTableView registerClass:[DaysHistogramViewCell class] forCellReuseIdentifier:DaysHistogramViewCellID];
    [self addSubview:_trendTableView];
    [_trendTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
    
}
- (void)loadNewData
{
    [self.loadNetView start];
    if (!_param) {
        _param = @{
                   @"startDate":[NSString getDateString3],
                   @"endDate":[NSString getCurrentDate2]
                   };
    }
//    [YJProgressHUD hide];
//    [YJProgressHUD showProgress:@"加载中..." inView:self];
    
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
//    hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
//    //[YJProgressHUD shareinstance].hud.dimBackground = YES;    //是否显示透明背景
//    hud.color = [UIColor blackColor];
//    [hud setMargin:10];
//    [hud setRemoveFromSuperViewOnHide:YES];
//    //        hud.detailsLabel.text = msg;
//    hud.contentColor = [UIColor whiteColor];
//    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_trendByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
         [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        [YJProgressHUD hide];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            NSDictionary *dict = responseObject[@"obj"];
            self.countOnlineCarByDaily = dict[@"countOnlineCarByDaily"];
            self.countUseCarByDaily = dict[@"countUseCarByDaily"];
            self.increasedTrend = dict[@"increasedTrend"];
            _allowLoad = YES;
        }
        [_trendTableView reloadData];
        [_trendTableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
         [_trendTableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
   
}


- (void)loadDataBackGroundWithParam:(NSDictionary *)param
{
    if (_needRefresh) {
        if (!_param) {
            _param = @{
                       @"startDate":[NSString getDateString3],
                       @"endDate":[NSString getCurrentDate2]
                       };
        }
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_trendByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.countOnlineCarByDaily = dict[@"countOnlineCarByDaily"];
                self.countUseCarByDaily = dict[@"countUseCarByDaily"];
                self.increasedTrend = dict[@"increasedTrend"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_trendTableView.mj_header endRefreshing];
            [_trendTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_trendTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}
- (void)loadDataWithParam:(NSDictionary *)param
{
    if (_needRefresh) {
        if (!_param) {
            _param = @{
                       @"startDate":[NSString getDateString3],
                       @"endDate":[NSString getCurrentDate2]
                       };
        }
        [self.loadNetView start];
//        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
//        hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
//        //[YJProgressHUD shareinstance].hud.dimBackground = YES;    //是否显示透明背景
//        hud.color = [UIColor blackColor];
//        [hud setMargin:10];
//        [hud setRemoveFromSuperViewOnHide:YES];
//        //        hud.detailsLabel.text = msg;
//        hud.contentColor = [UIColor whiteColor];
//        hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_trendByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.countOnlineCarByDaily = dict[@"countOnlineCarByDaily"];
                self.countUseCarByDaily = dict[@"countUseCarByDaily"];
                self.increasedTrend = dict[@"increasedTrend"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_trendTableView.mj_header endRefreshing];
            [_trendTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_trendTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
        return 3;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        DaysHistogramViewCell *newCell = [DaysHistogramViewCell new];
        newCell.title = @"新增车辆数";
        if (self.increasedTrend) {
            [newCell laodUIWithDataArr:self.increasedTrend];
        }
        return newCell;
    }else if (indexPath.row == 1){
        DaysGraphViewCell *risiCell = [DaysGraphViewCell new];
        risiCell.title = @"车辆上线趋势";
        risiCell.tipTitle = @"车辆上线数";
        if (self.countOnlineCarByDaily) {
            [risiCell laodGraphWithDataArr:self.countOnlineCarByDaily andType:@"1"];
        }
        return risiCell;
    }else{
        DaysGraphViewCell *useCell = [DaysGraphViewCell new];
        useCell.title = @"车辆使用趋势";
        useCell.tipTitle = @"车辆使用数";
        if (self.countUseCarByDaily) {
            [useCell laodGraphWithDataArr:self.countUseCarByDaily andType:@"2"];
        }
        return useCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 269;
    }else if(indexPath.row == 1){
        return 269;
    }else{
        return 269;
    }
}
@end
